home *** CD-ROM | disk | FTP | other *** search
- ; FixPtToString.asm
- ;-------------------
- ; by Mike™ Scanlin 28 Dec 1986
-
- Xref FixPtToString,NumToString
-
- ;============
- FixPtToString
- ;============
- ; convert a 32 bit fixed point number into a pascal string
- ; input: D0 fixed point number
- ; D1 16 bit divisor used when D0 was calculated
- ; D2 # of digits after decimal point (D2=0 for no dec point)
- ; A0 points to a space of at least (8 + D2) bytes
- ; output: A0 points to pascal string
-
- MOVEM.L D0-D3/A1,-(SP)
- MOVE.L D0,D3 ;save quotient & remainder
- EXT.L D0 ;sign extend quotient
- JSR NumToString
- ;if q = 0 and the result should be < 0, we'll have to add a minus sign
- ;(NumToString won't know about it, since all it sees is a zero quotient)
- TST D3 ;q = 0?
- BNE.S @0
- TST.L D3 ;check remainder
- BEQ.S @0 ;q & r both zero
- ;if r & divisor have the same sign, then result will be > 0
- EXT.L D1
- MOVE.L D1,D0
- EOR.L D3,D0
- BPL.S @0
- MOVE.B #'-',1(A0)
- MOVE.B #'0',2(A0)
- MOVE.B #2,(A0) ;new length
- @0 TST D2 ;do we want a decimal point?
- BEQ.S @6
- MOVEQ #0,D0
- MOVE.B (A0),D0 ;length of quotient
- LEA 1(A0,D0),A1 ;end of string + 1
- MOVE.B #'.',(A1)+
- TST.L D1 ;make divisor positive
- BPL.S @1
- NEG.L D1
- @1 TST.L D3 ;make remainder positive
- BPL.S @2
- NEG.L D3
- @2 SWAP D3
- ANDI.L #$FFFF,D3 ;isolate remainder
- SUBQ #1,D2 ;loop control
- @3 ADD.L D3,D3 ;mult r by 10
- MOVE.L D3,D0
- ADD.L D0,D0 ;4x
- ADD.L D0,D0 ;8x
- ADD.L D0,D3 ;10x = 8x + 2x
- MOVEQ #'0',D0 ;init digit
- @4 CMP.L D1,D3 ;is 10r > divisor?
- BLT.S @5
- ADDQ #1,D0 ;increase digit
- SUB.L D1,D3 ;subtract divisor
- BNE.S @4
- @5 MOVE.B D0,(A1)+ ;add to string
- DBRA D2,@3
- MOVE A1,D0 ;calc length of new string
- SUB A0,D0
- SUBQ.B #1,D0 ;minus 1 for length byte
- MOVE.B D0,(A0)
- @6 MOVEM.L (SP)+,A1/D0-D3
- RTS
-